package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.BaseContext;
import com.reggie.common.R;
import com.reggie.dto.OrdersDto;
import com.reggie.entity.OrderDetail;
import com.reggie.entity.Orders;
import com.reggie.entity.ShoppingCart;
import com.reggie.service.OrderDetailService;
import com.reggie.service.OrdersService;
import com.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 添加订单 提交订单
     */
    @PostMapping("/submit")
    public R<String> orderSubmit(@RequestBody Orders orders){
        log.info("提交订单: {}",orders );
        //开始的时候 只传递了三个值
//        {
//            "remark": "",//备注
//                "payMethod": 1,//支付方式
//                "addressBookId": "11"//地址id
//        }都是orders里面的属性 点的什么菜 都在购物车里面有的 不用再传递 这里传递的都是订单所需的
        //过程太麻烦 写一个方法来实现
        ordersService.submit(orders);
        return R.success("提交订单成功!");
    }


    /**
     * 订单分页查询
     */
    @GetMapping("/userPage")
    public R<Page> selectPage(int page,int pageSize){
        log.info("移动端订单分页查询"+page+"-"+pageSize);
        Page selectPage = ordersService.selectPage(page, pageSize);
        return R.success(selectPage);
    }

    /**
     * 再来一单
     */
    @PostMapping("/again")
    @Transactional
    public R<String> subumitAgain(@RequestBody Orders orders){
        //再来一单是添加购物车
        LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderDetail::getOrderId,orders.getId());
        List<OrderDetail> orderDetails = orderDetailService.list(lqw);
        for (OrderDetail orderDetail : orderDetails) {
            ShoppingCart shoppingCart= new ShoppingCart();
            shoppingCart.setName(orderDetail.getName());
            shoppingCart.setImage(orderDetail.getImage());
            shoppingCart.setDishId(orderDetail.getDishId());
            shoppingCart.setSetmealId(orderDetail.getSetmealId());
            shoppingCart.setDishFlavor(orderDetail.getDishFlavor());
            shoppingCart.setAmount(orderDetail.getAmount());
            shoppingCart.setCreateTime(LocalDateTime.now());
            for (int i = orderDetail.getNumber(); i >= 1 ; i--) {
                shoppingCartService.shopadd(shoppingCart);
            }
        }
        return R.success("再来一单成功!");
    }



    /**
     * 管理端订单明细分页查询
     */
    @GetMapping("/page")
    public R<Page> selectPageAdmin(int page,int pageSize){
        log.info("管理端订单分页查询"+page+"-"+pageSize);
        Page selectPage = ordersService.selectPageAdmin(page, pageSize);
        return R.success(selectPage);
    }

    /**
     * 管理端订单派送
     */
    @PutMapping()
    public R<String> orderSpeed(@RequestBody Orders orders){
        Orders orders1 = ordersService.getById(orders);
        orders1.setStatus(orders.getStatus());
        ordersService.updateById(orders1);
        return R.success("订单状态更新成功!");
    }


}
